<template>
  <div>
    <PathThink
      :pathConfig="pathConfig"
      :leftNodeLists="leftNodeLists"
      :centerNodeLists="centerNodeLists"
      :rightNodeLists="rightNodeLists"/>
  </div>
</template>

<script>
import PathThink from "@/components/PathThink"

export default {
  components: {
    PathThink,
  },
  data() {
    return {
      id: '',
      pathConfig: {
        id: '',
        name: '',
        type: 1
      },
      leftNodeLists: [],
      centerNodeLists: [],
      rightNodeLists: [],
      nameList: [
        "原材料获取",
        "生产制造",
        "分销",
        "使用",
        "废弃",
      ],
      LeftNameList: ['原材料/物料', "耗材", "能源", "资源", "运输/通勤", "固碳", "包装材料"]
    }
  },
  mounted() {
    if (this.$route.query.id) {
      // 初始化模型元素
      this.id = this.$route.query.id
      this.initPath()
    }
  },
  methods: {
    // 设置初始信息
    initPath() {
      this.$API.product.getProcessModel({
        id: this.id
      }).then(response => {
        console.log('response')
        console.log(response)
        var data = response.data
        this.pathConfig.name = data.productManagementDO.name
        this.pathConfig.type = data.calculationModelDO.lifecycle ? 1 : 0
        this.pathConfig.id = this.id
        this.initItem(data.lists)
        console.log('this.pathConfig')
        console.log(this.pathConfig)
      })
    },
    // 初始化工序信息
    initItem(li) {
      // 初始化列表
      if (this.pathConfig.type == 1) {
        this.leftNodeLists = [[], [], [], [], []]
        this.rightNodeLists = [[], [], [], [], []]
        this.centerNodeLists = [[], [], [], [], []]
      } else {
        this.leftNodeLists = [[], [], [], []]
        this.rightNodeLists = [[], [], [], []]
        this.centerNodeLists = [[], [], [], []]
      }
      // 解构填入结点
      for (var i = 0; i < li.length; i++) {
        var typeIndex = this.nameList.indexOf(li[i].processPMResVO.periodName)
        this.leftNodeLists[typeIndex].push([])
        this.rightNodeLists[typeIndex].push([])
        this.centerNodeLists[typeIndex].push(li[i].processPMResVO)
        var processIndex = this.leftNodeLists[typeIndex].length - 1;

        for (var j = 0; j < li[i].emissions.length; j++) {
          if (this.LeftNameList.indexOf(li[i].emissions[j].type) != -1) {
            console.log("left")
            this.leftNodeLists[typeIndex][processIndex].push(li[i].emissions[j])
          } else {
            console.log("right")
            this.rightNodeLists[typeIndex][processIndex].push(li[i].emissions[j])
          }
        }
      }
      console.log("result:")
      console.log(this.leftNodeLists)
      console.log(this.centerNodeLists)
      console.log(this.rightNodeLists)

    }

  }
}
</script>

<style>

</style>
